

$("#addProjectsBtn").click(function () {
    // console.log(123);
    const name = $("#addProjectsName").val();

    $.ajax({
        url: '/subjects/createSubjects',
        type: 'POST',
        data: {
            name
        },
        success: function (res) {
            alert('专业数据新增成功')
            // console.log('res', res);
            subjectsInit();

        }
    })
})

async function subjectsInit() {
    const subjectsData = await getSubjectsData();
    console.log(subjectsData);
    subjectsRrender(subjectsData);
    subjectsSelectRender(subjectsData);
    const classesname = await getClassesBySubjectsId(subjectsData[0]._id);
    // console.log(subjectsData[0]._id);
    console.log(classesname);
    
    // classesRD(classesname.result);
}

subjectsInit();
function getSubjectsData() {
    return new Promise((resolve, reject) => {
        $.ajax({
            url: '/subjects/getSubjects',
            type:"GET",
            success(res) {
                if (res.code) {
              
                    resolve(res.result);
                } else {
                    reject();
                }
            }
        })
    })
}

function subjectsRrender(data) {
    // console.log(data);
    const subjectsHtml = data.map(item => {
        return `
        <tr>
        <td>${item._id}</td>
        <td>${item.name}</td>
        <td>
        <button data-id="${item._id}">修改</button>
        <button data-id="${item._id}">删除</button>
        </td>
    
    </tr>
        `
    }).join('');
    $("#subjectsTb").html(subjectsHtml)
}



function subjectsSelectRender(data) {
    // console.log(data);
    const selectHeml = data.map(item => {
        // console.log(item);
        return ` <option value="${item._id}">${item.name}</option>`
    }).join('');
    $("#selectHeadTeacher").html(selectHeml);
    $("#selectMajor").html(selectHeml);
    $("#subjectId").html(selectHeml);
    // $("#xzzy").html(selectHeml);
}

// function classesRD(data) {
//     const selectClassesHtml = data.map(item => `<option value="${item._id}">${item.name}</option>`).join('');
  
//     $("#selectClasses").html(selectClassesHtml)
// }